<?php

namespace App\Http\Controllers\User;

use App\Models\Company;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Response;
use App\Models\Achievement;
use Carbon\Carbon;
use App\Models\Attendance;
use Illuminate\Support\Facades\Auth;
class UserController extends BaseController
{


    public function UserEdit(){
        if(strpos($this->user->old_corp_no ,"web_") === false){
            $view = 'frontuser.user';//企业微信模板
        }else{
            $view = 'frontuser.user-set';//web通用模板
        }
        return view($view)->with('user',$this->user)->with('fuser',Auth::user());
    }


    public function userLogo(Request $request)
    {

        $img = $request->input('dataURL','');

        if(empty($img)){
            return Response::json(array('status'=>'fail','msg'=>'图像不能为空！'));
        }
        if (strstr($img,",")){
            $img = explode(',',$img);
            $img = $img[1];
        }
        $imageName = $this->user->id;
        $logo = "uploads/images/user_logo/".$imageName.'.png';
        $r = file_put_contents(public_path($logo), base64_decode($img));
        if (!$r) {
            return Response::json(array('status'=>'fail','msg'=>'图像上传失败！'));
        }else{
            $userlogo = User::find($this->user->id);
            $userlogo->user_logo = $logo;
            $userlogo->save();
            return Response::json(array('status'=>'success','data'=>$logo,'msg'=>'头像上传成功！'));
        }
    }

    public function achievement(Request $request){
        $uid = $request->input('uid');
        if(!is_positive_int($uid)){
            $uid = $this->user->id;
        }
        $user = User::find($uid);
        $end=Carbon::now()->toDateTimeString();
        $start=Carbon::now()->firstOfMonth()->toDateTimeString();
        $year_param = $request->input('year', 0);
        $company = Company::where('id',$this->user->corpid)->first();
        if(!is_positive_int($year_param)){
            $year_param = getCurrentYear();
        }
        if(!in_array($year_param,explode(',',$company->years_array))){
            $year_param = $company->from_year;
        }
        //$Achievements=Achievement::where('user_id',$this->user->id)->whereBetween('created_at',[$start,$end])->get();
        $Achievements=Achievement::where('user_id',$uid)->where('year_param',$year_param)->orderBy('month_param','desc')->orderBy('created_at','desc')->get();
        $sr=0;
        $st=0;
        foreach ($Achievements as $v){
            $sr+=$v->score;
            $st+=$v->standard;
        }
        $average=0;
        if ($sr>0){
            $average=round($sr/$st*100,2);
        }
        return view("frontuser.achievement")
            ->with('Achievements',$Achievements)
            ->with('user',$user)
            ->with('year_param',$year_param)
            ->with('average',$average);
    }

    public function attendance(Request $request){

        $uid = $request->input('uid');
        if(!is_positive_int($uid)){
            $uid = $this->user->id;
        }
        $user = User::find($uid);
        $year_param = $request->input('year', 0);
        $company = Company::where('id',$this->user->corpid)->first();
        if(!is_positive_int($year_param)){
            $year_param = getCurrentYear();
        }
        if(!in_array($year_param,explode(',',$company->years_array))){
            $year_param = $company->from_year;
        }
        //$time= Carbon::now()->toDateString();
        $time= date('Y-m-d',strtotime($year_param.'0101'));
        $event=$this->getEvent($time,$uid);
        $count=$this->getEventscore($time,$uid);


        return view("frontuser.attendance")->with('year_param',$year_param)->with('user',$user)->with('id',$uid)->with('count',$count)->with('event',json_encode($event));
    }

    protected function getEvent($time,$id){
        $start = strtotime ('first day of '.$time);
        $end = strtotime ('last day of '.$time);
        $data=Attendance::where(['user_id'=>$id])->whereBetween('time', [$start ,  $end])->get();
        $event=[];
        foreach ( $data as $value){
            $event[$value->time]=[];
            $Q1= ($value->late||$value->early||$value->absenteeism||(float)$value->business||(float)$value->sick || $value->chuchai || $value->qita);
            $Q2= $value->public ;
            $Q3=$value->overtime ;
            $Q1&&$Q2&&$Q3&&array_push($event[$value->time],6);
            !$Q1&&$Q2&&$Q3&&array_push($event[$value->time],5);
            $Q1&&!$Q2&&$Q3&&array_push($event[$value->time],4);
            $Q1&&$Q2&&!$Q3&&array_push($event[$value->time],3);
            !$Q1&&!$Q2&&$Q3&&array_push($event[$value->time],2);
            !$Q1&&$Q2&&!$Q3&&array_push($event[$value->time],1);
            $Q1&&!$Q2&&!$Q3&&array_push($event[$value->time],0);
        }
        return  $event;
    }

    protected function getEventscore($time,$id){
        $start = strtotime ('first day of '.$time);
        $end = strtotime ('last day of '.$time);
        $data=Attendance::where(['user_id'=>$id])->whereBetween('time', [$start ,  $end])->get();
        // dd($data);
        $score=0;
        foreach ( $data as $value){
            $score+=$value->score;
        }

        return  $score;
    }

    public function ajaxAttendanceInfo(Request $request){
        $id = $request->input('id',0);
        if(!is_positive_int($id)){
            $id = $this->user->id;
        }
        $time=$request->input('time');
       // $data=Attendance::where(['user_id'=>$this->user->id])->where('time',$time)->first();
        $data=Attendance::where(['user_id'=>$id])->where('time',$time)->first();
        return  $this->formatJson('success','修改成功',$data);

    }
}
